package user;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class User_Modify
 */
@WebServlet("/User_Modify")
public class User_Modify extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public User_Modify() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		User user = new User();
		response.setContentType("text/html; charset=UTF-8");
		System.out.println(request.getParameter("uid"));
		System.out.println(request.getParameterValues("favourite"));
		user.setUid(Integer.parseInt(request.getParameter("uid")));
		user.setSex(request.getParameter("sex"));
		user.setProfession(request.getParameter("profession"));
		user.setNote(request.getParameter("note"));
		String[] favourite_a = request.getParameterValues("favourite");//获取爱好
		String favourite = "";
		for(int i = 0; i <favourite_a.length;i++){
			favourite += favourite_a[i];
					}
		user.setFavourite(favourite);
		UserDao userDao = new UserDao();
		int result = userDao.doUpdateUser(user);
		//处理结构
				if(result >= 0) {
					response.getWriter().print("<script type=\"text/javascript\">alert(\"修改成功！\");window.location=\"/Lesson_310/content/welcome.jsp\";</script>");
					return;
				}else {
					response.sendRedirect("/Lesson_310/user/user_success.jsp?count=6");
				}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
